home *** CD-ROM | disk | FTP | other *** search
- goto example1
- ' The QuickBASIC Tutorial Series
-
- ' Lesson 10
- ' QB2 Programming Techniques
-
- ' Copyright 1986 Ford Software
-
- ' 4845 Willowbend Blvd
- ' Houston, TX 77035
- ' (713) 721-5205
- ' CompuServe ppn: 71355,470
-
- 'This Tutorial may not be copied or distributed in any form - printed, on disk
- 'or electronically - without the express written permission of Ford Software.
- ' Unlicensed distribution is a violation of copyright law
- ' and may be subject to civil and criminal prosecution.
-
- 'QuickBASIC is the registered trademark of the Microsoft Corporation.
- 'Ford Software is not associated in any way with the Microsoft Corporation.
-
- '(Make sure NumLock is off and press PgDn)
- 'page 2
- ' I N T R O D U C T I O N
-
- 'In this tutorial, we assume that you have some familiarity with BASIC and
- 'have used a BASIC interpreter before. If you have not, you should run the
- 'earlier lessons in this series before running this one. At the very least,
- 'you should first run TUTOR-01 which covers the functioning of the QB2 editor.
-
- 'The purpose of this lesson is to go over the features of QB2 that are
- 'different from the Microsoft (or IBM) BASIC or BASICA interpreter.
-
- 'To run examples in this lesson, first press Ctrl-PgUp to get to the first
- 'line of this file. Change the line to "goto example#" where "#" is the number
- 'of the example you wish to run. Then return to the page you were on and press
- 'Ctrl-R to compile and run the code.
-
- 'This tutorial is meant to be used in conjunction with the QB2 manaul. As each
- 'topic is introduced in this tutorial, look up that topic in the manual and
- 'read about it first. The purpose of this tutorial is to provide interactive
- 'reinforcement of the material in the manual and present in a logical order.
-
- '(Press PgDn)
- 'page 3
- ' Contents
- '
- ' page
- ' 4 Labels and Line Numbers
- ' 5 Long Lines
- ' 6 Code Readability
- ' 7 Program Structure
- ' 8 Getting Rid of GOTO's
- ' 8 ..Interpretive BASIC example
- ' 8 ..In QB2 format
- ' 10 Replacing IF-THEN With WHILE-WEND
- ' 11 Block IF-THEN-ELSEIF-ELSE-END_IF
- '
- '
- '
- ' To move directly to a particular page, press Ctrl-F and enter "page n"
- ' where "n" is the page number to go to. Then use Ctrl-Down to realign
- ' the page on the screen. To search for a topic, press Ctrl-F
- '
- '
- '(Press PgDn)
- 'page 4 Labels and Line Numbers
-
- 'QB2 does not require lines to be numbered as the BASIC interpreter does.
- 'Sometimes, however, you will need to be able to identify a line or section of
- 'code. You can use a line number just like when using the interpreter, or you
- 'can use a "label".
-
- 'A label can be up to 40 characters of alphanumeric characters. It must be the
- 'first non-blank character on the line and must be followed by a colon (:).
- 'You do NOT add the colon when using GOTO, GOSUB, etc., just when defining it.
- 'In a label, there is no difference between capital and non-capital letters.
- 'SORT: is the same as Sort:. Spaces are not allowed in a label. "SORT.DATA:"
- 'is a valid label. "SORT DATA:" is not.
-
- example1:
- TEXT$="Hello" 'This routine will call the subroutine by the
- GOSUB Print.text 'name of "Print.text" to print TEXT$.
- END
- Print.text: 'Press Ctrl-R to compile and run this routine.
- PRINT TEXT$
- RETURN
- ' (PgDn)
- 'page 5 Long Lines
-
- 'The BASIC interpreter limits lines to 255 characters. Some commands in BASIC,
- 'such as FIELD statements or IF-THEN-ELSE constructs, could easily take more
- 'than 255 characters. Under the interpreter, you have to find some way to
- 'break these into smaller lines.
-
- 'QB2 also limits lines in the editor ("physical lines") to 255 characters: 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250
- '23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
- 'QB2 allows you to combine several physical lines into one long program line
- 'of up to 32,767 characters (a "logical line"). Ending a physical line with
- 'an underscore character ("_") indicates that the logical line is continuing.
-
- 'Why should you want such a long line? In the interpreter, a common practice
- 'is to test for a value and use GOTO to branch around code if the test fails:
- '1000 IF X <> 1 THEN GOTO 1050 where the lines between 1000 and 1050 do a lot
- 'of stuff that you don't want done unless X=1. In QB2, you would say:
- ' IF X=1 THEN_
- ' A=B:_ 'Usage of GOTO can make code very difficult to follow.
- ' C=D:_ 'QB2's formatting capabilities make it a lot easier.
- ' E=F:_
- ' (etc) (Press PgDn)
- 'page 6 Code Readability
-
- 'In QB2, you can still enter code just like you did in the interpreter:
-
- ' INPUT "Enter 1-10"; X:FOR I=1 TO 10:IF I=X THEN PRINT I*5 ELSE PRINT I*2
- ' IF I=7 THEN PRINT X;:PRINT "I EQUALS 7"
- ' NEXT:END
-
- 'which is difficult to read, debug and modify. Or you can enter:
-
- example2:
- INPUT "Enter 1-10"; X ' Notice the use of "_" to continue a logical line
- FOR I=1 TO 10 ' onto the next line. A common mistake is to leave
- IF I=X_ ' off a "_" or to put one where you didn't really
- THEN PRINT "I*5" I*5_ ' want it. Run this example to see that it works.
- ELSE PRINT "I*2" I*2 ' Remove one of the "_" continuation lines and run
- IF I=7 THEN_ ' it, but before you do, notice what page number
- PRINT X;:_ ' this is so that you can find your way back here.
- PRINT "I EQUALS 7" ' Notice that you must also have a ":" before the
- NEXT ' "_" to separate statements on the same logical
- END ' line. Please study these two examples carefully.
- ' (PgDn)
- 'page 7 (Code Readability, continued)
-
- 'There are no hard and fast rules for making code more readable. I prefer to
- 'spread code out as much as possible; but there is also something to be said
- 'for combining small pieces of code onto one line so that you can see more of
- 'the code at once. For example, putting "X=1:Y=2:Z=3" onto one line does not
- 'impair readability that much and lets you see a couple of more lines on one
- 'screen.
-
- 'PROGRAM STRUCTURE:
- 'A program should be laid out much like a book. It should start with the title
- 'and author's name and copyright notice. There should be a version number and
- 'a few lines (Introduction) explaining the purpose of the program and listing
- 'any associated files. Then you should list the label of each major part of
- 'the program with a short description, similar to a Table of Contents.
-
- 'The first lines of actual code should be DIM and DEF statements, followed by
- 'GOSUBs to different routines such as to intialize program variables, open
- 'files, draw the screen, etc. Then go into your main program loop, which calls
- 'the subroutines. Load QPRINT to see an example of QB2 program structure.
-
- ' (PgDn)
- 'page 8 Getting Rid of GOTO's
-
- 'One of the major changes in going from interpretive BASIC to QB2 is getting
- 'rid of the GOTO's. Here is a typically snarled piece of code in BASIC:
- example3:
- 1000 INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
- 1010 IF X=1 THEN GOTO 1050
- 1020 FOR I=1 TO 10: IF X=I THEN PRINT 1 X
- 1030 NEXT
- 1040 GOTO 1060
- 1050 PRINT 1 "X=1"
- 1060 ' lines 1010-1050 in QB2 format:
- IF X=1_
- THEN PRINT 2 "X=1"_
- ELSE FOR I=1 TO 10:_
- IF X=I THEN PRINT 2 X:_ 'and here is where things break down.
- NEXT 'Although NEXT is on a seperate line, the "_" in the line above
- 'makes these two lines equivalent to
- '1020 FOR I=1 TO 10: IF X=I THEN PRINT X: NEXT 'Most people will recognize
- 'in this example that the NEXT will not be executed except when X=I, but the
- 'QB structure may actually make this more difficult to recognize because it
- 'is easy to overlook the "_" at the end of the previous line. (PgDn)
- 'page 9 (Getting Rid of GOTO's, continued)
-
- 'So how do we get around this? One way would be to branch around the rest of
- IF X=1 THEN_ ' the code if x=1, just as we did in the original
- PRINT 3 "X=1":_ ' code with line numbers. (But we are getting back to
- GOTO Branch.around ' the old "spagetti code" again when we do that.)
- FOR I=1 TO 10 ' Note that the lines on the left no longer have a _
- IF X=I THEN PRINT 3 X ' at the end of each one. That means that the NEXT no
- NEXT ' longer is on the same logical line as the IF X...
- Branch.around: ' Run "example3" (which starts on the prior page).
- END ' Enter "1" when prompted and you will see that all
- ' three sections of code work.
-
- 'Run it again and enter "3" at the prompt and notice that the second section
- 'of code does not work.
-
- 'The reason that the code above works when the code on the previous screen did
- 'not is because the IF line in the code above does not end with a "_". These
- 'marks are very easy to overlook and can cause some strange error messages.
-
-
- ' (PgDn)
- 'page 10 (Getting Rid of GOTO's, continued)
-
- 'To avoid having to branch around, you can use a WHILE-WEND test instead of a
- 'IF-THEN test. Here is the same code with a WHILE-WEND test:
- example4: INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
- IF X=1_
- THEN PRINT 4 "X=1"_ 'Press Ctrl-PgUp, change the first line to
- ELSE FOR I=1 TO 10:_ '"goto example4", and press Ctrl-R.
- WHILE I=X:_ 'Try entering 1 and 2.
- PRINT 4 X:_
- I=I+1:_ ' <- This line is added to keep the WHILE-WEND
- WEND:_ 'loop from being endless. Incrementing "I" to
- NEXT 'break out is a convenient method in this case.
- END 'You usually have to come up with something
- 'different each time. Sometimes, you have to create a new variable just for
- 'breaking out of a loop if you don't want to change the other variables:
- ' LOOP=1:_
- ' WHILE (I=X) AND (LOOP=1):_
- ' PRINT X:_
- ' LOOP=0:_ 'This example would have worked just as well
- ' WEND:_ 'in the code above as incrementing "I" did.
- ' (PgDn)
- 'page 11 (Getting Rid of GOTO's, continued)
-
- 'Yet another way to take conditional action in the middle of a long logical
- 'line is to GOSUB to a routine that will make the test and take the action if
- 'necessary. Using our example:
- example5: INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
- IF X=1_ ' Run this example to see that it works.
- THEN PRINT 5 "X=1":_ ' While this makes the main routine cleaner, it
- ELSE FOR I=1 TO 10:_ ' also can make following the code more difficult.
- GOSUB Test.I:_ ' Having a lot of little subroutines can muck up
- NEXT ' your code something terrible. You must use your
- END ' own judgement as to whether you should kludge up
- Test.I: ' your code with WHILE-WEND loops and extra varia-
- IF I=X THEN PRINT 5 I ' bles or branch to another subroutine as in this
- RETURN ' example.
-
- 'There is one more way around the problem. Most of the code you write with QB2
- 'can also be compiled without change by the IBM BASIC Compiler Version 2. One
- 'exception is the IF-THEN-ELSEIF-THEN-ELSE structure. As a result, you may
- 'prefer to not use this structure in your programs. If you are not worried
- 'about BASCOM2 compatibility, here is how this structure is used:
- ' (PgDn)
- 'page 12 (Getting Rid of GOTO's, continued)
-
- example6: INPUT "ENTER A SINGLE-DIGIT NUMBER"; X
- IF X=1 THEN
- PRINT "X=1"
- ELSE FOR I=1 TO 10 ' Note the lack of "_" line continuation
- IF I=X THEN PRINT I ' symbols in this code. That's because the
- NEXT ' END IF defines the end of the IF-THEN
- END IF ' processing, so all the code does not have
- END ' to be in the same logical line.
-
- 'In other examples, we have varied the form of the IF-THEN command to suit
- 'our own taste. To use an IF-END_IF block, you must use an exact structure:
-
- ' IF xxx THEN 'The THEN must be the last word on the line.
- ' <action>
- ' ELSEIF yyy THEN 'ELSEIF must be the first word on the line, but
- ' <action> 'ELSEIF is optional, as seen in the example above.
- ' ELSE <action>
- ' END IF
-
- ' end of lesson 10
- 'If you were running this program and ended up here at the end of the code
- 'because of some error, just press Alt-V, E, Enter to get rid of the error
- 'message box. If you think the program is badly messed up, enter Alt-F, L,
- 'Enter and reload the program name shown. Otherwise, just press Ctrl-F, enter
- 'the page number you were on and use Ctrl-Down to realign the page on the
- 'screen.
-